package com.tw.playerdata;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Random;

public class Player 
{
	private int currency = 0;
	HashMap<String,Skill> skills;
	
	public Player(int score)
	{
		addshutdowncheck();
		skills = new HashMap<String,Skill>();
		
		//damage
		skills.put("Intelligence", new Skill());
		skills.put("Strength", new Skill());
		//crit
		skills.put("Dexterity", new Skill());
		skills.put("Charisma", new Skill());
		//hp
		skills.put("Constitution", new Skill());
		
		generate(score);
	}

	public Player(boolean user,int score)
	{
		addshutdowncheck();

		skills = new HashMap<String,Skill>();
		
		//damage
		skills.put("Intelligence", new Skill());
		skills.put("Strength", new Skill());
		//crit
		skills.put("Dexterity", new Skill());
		skills.put("Charisma", new Skill());
		//hp
		skills.put("Constitution", new Skill());
	
		if(user)
		{
			File f = new File(".playerinfo");
			if(!f.exists())
			{
				try {
					f.createNewFile();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				this.generate(score);
			}
			else
			{
				loadplayerinfo(f);
			}
		}
		else
		{
			this.generate(score);
		}
	}
	public Player generate(int level)
	{
		Player p = this;
		
		Random r = new Random();
		
		while(p.getLevel("Constitution") + p.getLevel("Strength") + p.getLevel("Dexterity") + p.getLevel("Intelligence") + p.getLevel("Charisma") < level)
		{
			switch(r.nextInt() % 5)
			{
				case 0:
					p.giveExperience(1, "Constitution");
					break;
				case 1:
					p.giveExperience(1, "Strength");
					break;
				case 2:
					p.giveExperience(1, "Dexterity");
					break;
				case 3:
					p.giveExperience(1, "Intelligence");
					break;
				case 4:
					p.giveExperience(1, "Charisma");
					break;
			}
		}
		
		return p;
	}
	private void addshutdowncheck() {
		Inner i = new Inner();
		Runtime.getRuntime().addShutdownHook(i);
	}
	
	private class Inner extends Thread {
		public void run()
		{
			save();
		}
	}
	
	private void save()
	{
		File f = new File(".playerinfo");
		if(f.exists())
			f.delete();
		try {
			f.createNewFile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		BufferedWriter bw = null;
		
		try {
			bw = new BufferedWriter(new FileWriter(f));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if(bw != null)
		{
			for(String s : skills.keySet())
				try {
					bw.write(s+","+skills.get(s).getSaveString()+"\r\n");
					System.out.println("write"+s+","+skills.get(s).getSaveString());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			
			try {
				bw.write("Currency,"+currency+"\r\n");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(bw != null)
			try {
				bw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	
	private void loadplayerinfo(File f) {
		BufferedReader br = null;
		skills = new HashMap<String,Skill>();
		try {
			br = new BufferedReader(new FileReader(f));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String s;
		try {
			while(br != null && (s = br.readLine()) != null)
			{
				if(s.startsWith("Currency"))
					currency = Integer.parseInt(s.substring(s.indexOf(',')+1));
				else
					skills.put(s.substring(0, s.indexOf(',')), new Skill(s.substring(s.indexOf(',')+1)));
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(br != null)
			try {
				br.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}

	public boolean giveExperience(int experience, String skill)
	{
		if(skills.containsKey(skill))
			return skills.get(skill).giveExperience(experience);
		return false;
	}
	
	public int getLevel(String skill)
	{	
		if(skills.containsKey(skill))
			return skills.get(skill).getLevel();
		return -1;
	}
	
	public int giveCurrency(int togive)
	{
		currency+=togive;
		return currency;
	}
	
	public int takeCurrency(int totake)
	{
		if(currency >= totake)
			return currency -= totake;
		else return -1;
	}
	
	public int getCurrency()
	{
		return currency;
	}
	
	public HashMap<String,String> startBattle()
	{
		HashMap<String,String> bstats = new HashMap<String,String>();
		double x;
		
		bstats.put("totalhp", new Integer(skills.get("Constitution").getLevel()*5+5).toString());
		bstats.put("currenthp", bstats.get("totalhp"));
		if(skills.get("Intelligence").getLevel() > skills.get("Strength").getLevel())
		{
			bstats.put("dmg", new Integer(skills.get("Intelligence").getLevel() + 1).toString());
		}
		else
		{
			bstats.put("dmg", new Integer(skills.get("Strength").getLevel() + 1).toString());
		}
		if(skills.get("Dexterity").getLevel() > skills.get("Charisma").getLevel())
		{
			x = skills.get("Dexterity").getLevel();
			x = (x*.06)/Math.sqrt(Math.pow(x*.1,2)+1);
			bstats.put("crit", new Double(x).toString());
		}
		else
		{
			x = skills.get("Charisma").getLevel();
			x = (x*.06)/Math.sqrt(Math.pow(x*.1,2)+1);
			bstats.put("crit", new Double(x).toString());
		}
		x = skills.get("Intelligence").getLevel() + skills.get("Dexterity").getLevel();
		bstats.put("hitadd", new Double(x).toString());
		return bstats;
	}
	
	public void printStats()
	{
		System.out.println("Con: " + skills.get("Constitution").getLevel());
		System.out.println("Str: " + skills.get("Strength").getLevel());
		System.out.println("Dex: " + skills.get("Dexterity").getLevel());
		System.out.println("Int: " + skills.get("Intelligence").getLevel());
		System.out.println("Cha: " + skills.get("Charisma").getLevel());
		HashMap<String,String> battlestats = startBattle();
		for(String sk : battlestats.keySet())
		{
			System.out.println(sk + ": " + battlestats.get(sk));
		}
		
	}
	
	public static void main(String [] args)
	{
		Random r = new Random();
		Player p = new Player(15);
		while(p.getLevel("Constitution") + p.getLevel("Strength") + p.getLevel("Dexterity") + p.getLevel("Intelligence") + p.getLevel("Charisma") < 15)
		{
			switch(r.nextInt() % 5)
			{
				case 0:
					System.out.println("Adding 1 to con");
					if(p.giveExperience(1, "Constitution"))
					{
						System.out.println("DING");
						p.printStats();
					}
					break;
				case 1:
					System.out.println("Adding 1 to str");
					if(p.giveExperience(1, "Strength"))
					{
						System.out.println("DING");
						p.printStats();
					}
					break;
				case 2:
					System.out.println("Adding 1 to dex");
					if(p.giveExperience(1, "Dexterity"))
					{
						System.out.println("DING");
						p.printStats();
					}
					break;
				case 3:
					System.out.println("Adding 1 to int");
					if(p.giveExperience(1, "Intelligence"))
					{
						System.out.println("DING");
						p.printStats();
					}
					break;
				case 4:
					System.out.println("Adding 1 to cha");
					if(p.giveExperience(1, "Charisma"))
					{
						System.out.println("DING");
						p.printStats();
					}
					break;
			}
		}
	}
}
